package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.TreeBuildUtils;
import com.xmtlxx.wms.domain.Cabinet;
import com.xmtlxx.wms.domain.CabinetGroup;
import com.xmtlxx.wms.domain.Locator;
import com.xmtlxx.wms.domain.bo.CabinetGroupBo;
import com.xmtlxx.wms.domain.vo.CabinetGroupVo;
import com.xmtlxx.wms.mapper.CabinetGroupMapper;
import com.xmtlxx.wms.mapper.CabinetMapper;
import com.xmtlxx.wms.mapper.LocatorMapper;
import com.xmtlxx.wms.mapper.StoreMapper;
import com.xmtlxx.wms.service.ICabinetGroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 智能柜Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-12-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CabinetGroupServiceImpl implements ICabinetGroupService {

    private final CabinetGroupMapper baseMapper;

    private final StoreMapper storeMapper;

    private final LocatorMapper locatorMapper;

    private final CabinetMapper cabinetMapper;

    /**
     * 查询智能柜
     *
     * @param id 智能柜主键
     * @return 智能柜
     */
    @Override
    public CabinetGroupVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectCabinetGroupVoByID(id);
    }

    @Override
    public CabinetGroup getById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int updateByEntity(CabinetGroup enty) {
        return baseMapper.updateById(enty);
    }

    /**
     * 查询智能柜列表
     *
     * @param bo 智能柜
     * @return 智能柜
     */
    @Override
    public TableDataInfo<CabinetGroupVo> queryPageList(CabinetGroupBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<CabinetGroup> lqw = buildQueryWrapper(bo);
       // Page<CabinetGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<CabinetGroupVo> result = baseMapper.selectPageCabinetGroupList(pageQuery.build(), bo);
        if(result != null && result.getRecords() != null && result.getRecords().size() > 0){
            for (CabinetGroupVo vo:result.getRecords()){
               if(vo.getType().equals("1")){
                   vo.setIsShow(true);
               }else {
                   vo.setIsShow(false);
               }
               vo.setIsSync(false);
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询智能柜列表
     *
     * @param bo 智能柜
     * @return 智能柜
     */
    @Override
    public List<CabinetGroupVo> queryList(CabinetGroupBo bo) {
       // LambdaQueryWrapper<CabinetGroup> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectCabinetGroupList(bo);
    }

    private LambdaQueryWrapper<CabinetGroup> buildQueryWrapper(CabinetGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CabinetGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getId()), CabinetGroup::getId, bo.getId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), CabinetGroup::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCno()), CabinetGroup::getCno, bo.getCno());
        lqw.eq(StringUtils.isNotBlank(bo.getSnNumber()), CabinetGroup::getSnNumber, bo.getSnNumber());
        lqw.eq(bo.getDeptId() != null, CabinetGroup::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreId()), CabinetGroup::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), CabinetGroup::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), CabinetGroup::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CabinetGroup::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增智能柜
     *
     * @param bo 智能柜
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(CabinetGroupBo bo) {
        CabinetGroup add = BeanUtil.toBean(bo, CabinetGroup.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        add.setDeptId(storeMapper.selectVoById(add.getStoreId()).getDeptId());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改智能柜
     *
     * @param bo 智能柜
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(CabinetGroupBo bo) {
        CabinetGroup update = BeanUtil.toBean(bo, CabinetGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CabinetGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除智能柜
     *
     * @param ids 需要删除的智能柜主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        LambdaQueryWrapper<Cabinet> cabinetCons = new LambdaQueryWrapper<Cabinet>();
        cabinetCons.in(Cabinet::getGroupId,ids);
        int flag = cabinetMapper.delete(cabinetCons);

        LambdaQueryWrapper<Locator> locatorCons = new LambdaQueryWrapper<Locator>();
        locatorCons.in(Locator::getCabinetId,ids);
        flag = locatorMapper.delete(locatorCons);

        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<Tree<String>> buildCabinetGroupTreeSelect(List<CabinetGroupVo> items) {
        if (CollUtil.isEmpty(items)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(items, (store, tree) ->
            tree.setId(store.getId())
                .setParentId(store.getParentId())
                .setName(store.getName())
                .setWeight(store.getId()));
    }
}
